#include "lcd12864.h"


void LCD12864_Delay1ms(uint c)
{
    uchar a,b;
	for(; c>0; c--)
	{
	    for(b=199; b>0; b--)
		{
	        for(a=1; a>0; a--);
		}
	}
}


void LCD12864_WriteCmd(uchar cmd)
{
	uchar i;
	i = 0;
	
	LCD12864_Delay1ms(20);
	
	LCD12864_RS = 0;     //????
	LCD12864_RW = 0;     //????
	LCD12864_EN = 0;     //??????

	LCD12864_DATAPORT = cmd;   //????

	LCD12864_EN = 1;		   //???
	LCD12864_Delay1ms(5);
	LCD12864_EN = 0;    					
}


void LCD12864_WriteData(uchar dat)
{
	uchar i;
	i = 0;

	LCD12864_Delay1ms(20);

	LCD12864_RS = 1;     //????
	LCD12864_RW = 0;     //????
	LCD12864_EN = 0;     //??????

	LCD12864_DATAPORT = dat;   //????

	LCD12864_EN = 1;		   //???
	LCD12864_Delay1ms(5);
	LCD12864_EN = 0;    								
}

#ifdef LCD12864_PICTURE

uchar LCD12864_ReadData(void)
{
	uchar i, readValue;

	i = 0;
	
	LCD12864_Delay1ms(20);

	LCD12864_RS = 1;      
	LCD12864_RW = 1;
	LCD12864_EN = 0;
	LCD12864_Delay1ms(1);  

	LCD12864_EN = 1;
	LCD12864_Delay1ms(1);
	readValue = LCD12864_DATAPORT;
	LCD12864_EN = 0;

	return readValue;
}








void LCD12864_ClearScreen(void)
{
	uchar i,j;

	LCD12864_WriteCmd(0x34);

	for(i=0;i<32;i++)			
	{
		LCD12864_WriteCmd(0x80+i);		
		LCD12864_WriteCmd(0x80);	
		for(j=0;j<32;j++)		
		{						 
			LCD12864_WriteData(0xFF);
		}
	}

	LCD12864_WriteCmd(0x36);
	LCD12864_WriteCmd(0x30);
}



void LCD12864_SetWindow(uchar x, uchar y)
{
	uchar pos;

	if(x == 0)	 
	{
		x = 0x80;
	}
		
	else if(x == 1) 
	{
		x = 0x90;	
	}
	
	else if(x == 2)
	{
		x = 0x88;
	}
	
	else if(x == 3)
	{
		x = 0x98;
	}
	
	pos = x + y;
	LCD12864_WriteCmd(pos);
}
